function hfile = aft_SaveAs(hfile, newfile, dtitle)
% AnyFileType::SaveAs  - method for any BVQXfile type
%
% FORMAT:       obj.SaveAs([newfilename, dtitle]);
%
% Input fields:
%
%       newfilename save-as filename, if not give, UI-based
%       dtitle      if given, override UI default title
%
% No output fields:
%
%       obj         BVQXfile object with newly set filename
%
% TYPES: ALL

% Version:  v0.8a
% Build:    9102122
% Date:     Oct-21 2009, 10:09 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin < 1 || ...
    numel(hfile) ~= 1 || ...
   ~isBVQXfile(hfile, true)
    error( ...
        'BVQXfile:BadArguments', ...
        'Invalid call to %s.', ...
        mfilename ...
    );
end

% get objects super-struct
sc = bvqxfile_getscont(hfile.L);

ndtitle = '';
if nargin > 1 && ...
    ischar(newfile) && ...
   ~any(newfile == '.')
    ndtitle = newfile(:)';
    newfile = '';
end
if nargin < 2 || ...
   ~ischar(newfile) || ...
    isempty(newfile)
    if isempty(sc.F)
        newfile = ['*.' sc.S.Extensions{1}];
    else
        [of{1:3}] = fileparts(sc.F);
        newfile = ['*' of{3}];
    end
end
if nargin < 3 || ...
   ~ischar(dtitle) || ...
    isempty(dtitle)
    if ~isempty(ndtitle)
        dtitle = ndtitle;
    else
        dtitle = 1;
    end
end
newfile = newfile(:)';

% make absolute name
[isabs{1:2}] = isabsolute(newfile);
newfile = isabs{2};
[fnparts{1:3}] = fileparts(newfile);

% check for "*.???"
if isempty(fnparts{2})
    fnparts{2} = '*';
end
if isempty(fnparts{3}) || ...
    strcmp(fnparts{3}, '.')
    fnparts{3} = ['.' sc.S.Extensions{1}];
end
if any(fnparts{2} == '*')
    extensions   = BVQXfile(0, 'extensions');
    file_formats = BVQXfile(0, 'formats');
    filename = bvqxfile_requestfile(dtitle, ...
        [fnparts{1} filesep fnparts{2} fnparts{3}], ...
        extensions, file_formats, true);
    if isempty(filename)
        warning( ...
            'BVQXfile:OperationCancelled', ...
            'SaveAs dialog cancelled.' ...
        );
        return;
    end
    if iscell(filename)
        filename = filename{1};
    end
    [isabs{1:2}] = isabsolute(filename);
    newfile = isabs{2};
end
    
% what to do
try
    switch (lower(sc.S.FFTYPE))
        case {'bff'}
            sc.C = bffio(newfile, sc.S, sc.C);
            sc.F = newfile;
        case {'tff'}
            [sc.C, sc.F] = tffio(newfile, sc.S, sc.C);
        otherwise
            error( ...
                'BVQXfile:InvalidFileType', ...
                'Type not recognized (?FF): %s.', ...
                sc.S.FFTYPE ...
            );
    end
catch
    error( ...
        'BVQXfile:ErrorSavingFile', ...
        'Error saving file %s: %s.', ...
        sc.F(:)', lasterr ...
    );
end

% set new filename and maybe also content
bvqxfile_setscont(hfile.L, sc);
